﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Srv3CodeTool
{
    public partial class FrmAdapterInterface : Form
    {
        public FrmAdapterInterface()
        {
            InitializeComponent();
        }

        private Config config = new Config();
        private void reFlashCmbBox(ComboBox cmbBox, string szModule)
        {
            try
            {
                if (szModule == null || szModule == "")
                    return;
                cmbBox.Items.Clear();
                StreamReader objReader = new StreamReader(szModule + "\\Src\\Provider.h");
                if (objReader == null)
                {
                    //log
                    return;
                }
                string szLine = "";
                bool bFuncStart = false;
                while (szLine != null)
                {
                    szLine = objReader.ReadLine();
                    if (szLine == null)
                        continue;
                    if (!bFuncStart && szLine.IndexOf("CProvider") >= 0)
                    {
                        string szClassFunc = objReader.ReadLine();
                        if (szClassFunc.IndexOf("{") >= 0 || szLine.IndexOf("{") >= 0)
                        {//类定义开始
                            bFuncStart = true;
                        }
                    }

                    if (bFuncStart && szLine.IndexOf("}") >= 0)
                    {//类定义结束
                        break;
                    }

                    if (!bFuncStart)
                        continue;

                    // 此行有注释符
                    int nCommentIndex = szLine.IndexOf("////");
                    int nFuncStartIndex = szLine.IndexOf("(");
                    if (nFuncStartIndex < 0)
                        continue;
                    if (nCommentIndex > 0 && nCommentIndex < nFuncStartIndex)
                        continue;

                    string szFuncTmp = szLine.Substring(0, nFuncStartIndex);
                    int nFuncBeforeIndex = Math.Max(szFuncTmp.LastIndexOf(' '), szFuncTmp.LastIndexOf('*'));
                    nFuncBeforeIndex = Math.Max(szFuncTmp.LastIndexOf('\t'), nFuncBeforeIndex);
                    string szFunc = szFuncTmp.Substring(nFuncBeforeIndex + 1);
                    if (szFunc.IndexOf("CProvider") >= 0 || szFunc.IndexOf("~CProvider") >= 0)
                        continue;
                    cmbBox.Items.Add(szFunc);
                }
                objReader.Close();
                cmbBox.SelectedIndex = 0;
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Functions func = new Functions();
            try
            {
                if (cmbProviderModule.SelectedIndex == cmbConsumerModule.SelectedIndex)
                {
                    Functions.ShowMsgBox("相同模块不能转接口!", MSGBOX_TYPE.MSGBOX_TYPE_WARNING);
                    return;
                }
                if (func.AdapterInterfaceNew(cmbProviderModule.SelectedItem.ToString(), cmbProviderFunc.SelectedItem.ToString(),
                    cmbConsumerModule.SelectedItem.ToString(), txtConsumerFunc.Text.ToString()))
                {
                    Functions.ShowMsgBox("转换接口成功!", MSGBOX_TYPE.MSGBOX_TYPE_INFO);
                }
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }

        private void FrmAdapterInterface_Load(object sender, EventArgs e)
        {
            try
            {
                config.LoadFromXmlFile();
                string[] szModuleArr = Directory.GetDirectories(config.ModulePath);
                foreach (string szPath in szModuleArr)
                {
                    int nIndex = szPath.LastIndexOf('\\');
                    string szModule = szPath.Substring(nIndex + 1);
                    if (szModule.IndexOf(".") >= 0)
                        continue;
                    if (szModule.IndexOf("Adapter") >= 0 || szModule.IndexOf("Include") >= 0 || szModule.IndexOf("Action") >= 0)
                        continue;
                    
                    cmbProviderModule.Items.Add(szModule);
                    cmbConsumerModule.Items.Add(szModule);
                }
                cmbProviderModule.SelectedIndex = 0;
                cmbConsumerModule.SelectedIndex = 0;

                this.reFlashCmbBox(cmbProviderFunc, config.ModulePath + "\\" + cmbProviderModule.SelectedItem.ToString());
                this.reFlashCmbBox(cmbConsumerFunc, config.ModulePath + "\\" + cmbConsumerModule.SelectedItem.ToString());
                txtConsumerFunc.Text = cmbProviderFunc.SelectedItem.ToString();
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }

        private void cmbProviderModule_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                this.reFlashCmbBox(cmbProviderFunc, config.ModulePath + "\\" + cmbProviderModule.SelectedItem.ToString());
                txtConsumerFunc.Text = cmbProviderFunc.SelectedItem.ToString();
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }

        private void cmbConsumerModule_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                this.reFlashCmbBox(cmbConsumerFunc, config.ModulePath + "\\" + cmbConsumerModule.SelectedItem.ToString());
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }

        private void cmbProviderFunc_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                txtConsumerFunc.Text = cmbProviderFunc.SelectedItem.ToString();
            }
            catch (System.Exception ex)
            {
                Log.LogSave(ex.ToString(), LOGTYPE.LOGTYPE_ERROR);
            }
        }
    }
}
